/**
 * --------------------------------------------------------------------------
 * 工具类
 * Hi.js (v1.0.0): demo.js
 * require jquery
 * --------------------------------------------------------------------------
 */
;
(function($, window, document, undefined) {
    "use strict";

    $.hiui = {
        // 获取元素在数组的索引
        indexOfArray: function(a, o, id) {
            for (var i = 0, len = a.length; i < len; i++) {
                if (id == undefined) {
                    if (a[i] == o) {
                        return i;
                    }
                } else {
                    if (a[i][o] == id) {
                        return i;
                    }
                }
            }
            return -1;
        },
        // 删除数组元素
        removeArrayItem: function(a, o, id) {
            if (typeof o == 'string') {
                for (var i = 0, len = a.length; i < len; i++) {
                    if (a[i][o] == id) {
                        return a.splice(i, 1);
                    }
                }
            } else {
                var idx = this.indexOfArray(a, 0);
                if (idx != -1) {
                    a.splice(idx, 1);
                }
            }
        },
        // 添加数组元素
        addArrayItem: function(a, o, r) {
            var idx = this.indexOfArray(a, o, r ? r[o] : undefined);
            if (idx == -1) {
                a.push(r ? r : o);
            } else {
                a[idx] = r ? r : o;
            }
        },
        // 获取数组元素
        getArrayItem: function(a, o, id) {
            var idx = this.indexOfArray(a, o, id);
            return idx == -1 ? null : a[idx];
        },
        // 遍历
        forEach: function(data, deep, callback) {
            var nodes = [];
            for (var i = 0; i < data.length; i++) {
                nodes.push(data[i]);
            }
            while (nodes.length) {
                var node = nodes.shift();
                if (callback(node) == false) {
                    return;
                }
                if (deep && node.children) {
                    for (var i = node.children.length - 1; i >= 0; i--) {
                        nodes.unshift(node.children[i]);
                    }
                }
            }
        }
    };

    $.parser = {
        auto: true,
        onComplete: function(context) {},
        plugins: ['tooltip', 'pagination', 'tree'],
        parse: function(context) {
            var aa = [];
            for (var i = 0; i < $.parser.plugins.length; i++) {
                var name = $.parser.plugins[i];
                var r = $(name, context);
                if (r.length) {
                    if (r[name]) {
                        r.each(function() {
                            $(this)[name]($.data(this, 'options') || []);
                        });
                    } else {
                        aa.push({
                            name: name,
                            jq: r
                        });
                    }
                }
            }
            if (aa.length && window.hiui) {
                var names = [];
                for (var i = 0; i < aa.length; i++) {
                    names.push(aa[i].name);
                }
                hiui.load(names, function() {
                    for (var i = 0; i < aa.length; i++) {
                        var name = aa[i].name;
                        var jq = aa[i].jq;
                        jq.each(function() {
                            $(this)[name]($.data(this, 'options') || []);
                        });
                    }
                    $.parser.onComplete.call($.parser, context);
                });
            } else {
                $.parser.onComplete.call($.parser, context);
            }
        },
        parserValue: function(property, value, parent, delta) {
            delta = delta || 0;
            var v = $.trim(String(value || ''));
            var endchar = v.substr(v.length - 1, 1);
            if (endchar == '%') {
                v = parseFloat(v.substr(0, v.length - 1));
                if (property.toLowerCase().indexOf('width') >= 0) {
                    delta += parent[0].offsetWidth - parent[0].clientWidth;
                    v = Math.floor((parent.width() - delta) * v / 100.0);
                } else {
                    delta += parent[0].offsetHeight - parent[0].clientHeight;
                    v = Math.floor((parent.height() - delta) * v / 100.0);
                }
            } else {
                v = parseInt(v) || undefined;
            }
            return v;
        },
        parseOptions: function(target, properties) {
            var t = $(target);
            var options = {};

            var s = $.trim(t.attr('data-options'));
            if (s) {
                if (s.substring(0, 1) != '{') {
                    s = '{' + s + '}';
                }
                options = (new Function('return ' + s))();
            }
            $.map(['width', 'height', 'left', 'top', 'minWidth', 'maxWidth', 'minHeight', 'maxHeight'], function(p) {
                var pv = $.trim(target.style && (target.style[p] || ''));
                if (pv) {
                    if (pv.indexOf('%') == -1) {
                        pv = parseInt(pv);
                        if (isNaN(pv)) {
                            pv = undefined;
                        }
                    }
                    options[p] = pv;
                }
            });

            if (properties) {
                var opts = {};
                for (var i = 0; i < properties.length; i++) {
                    var pp = properties[i];
                    if (typeof pp == 'string') {
                        opts[pp] = t.attr(pp);
                    } else {
                        for (var name in pp) {
                            var type = pp[name];
                            if (type == 'boolean') {
                                opts[name] = t.attr(name) ? (t.attr(name) == 'true') : undefined;
                            } else if (type == 'number') {
                                opts[name] = t.attr(name) == '0' ? 0 : parseFloat(t.attr(name)) || undefined;
                            }
                        }
                    }
                }
                $.extend(options, opts);
            }
            return options;
        },
        parseVars: function() {
            var d = $('<div style="position:absolute;top:-1000px;width:100px;height:100px;padding:5px"></div>').appendTo('body');
            $._boxModel = d.outerWidth() != 100;
            d.remove();
            d = $('<div style="position:fixed"></div>').appendTo('body');
            $._positionFixed = (d.css('position') == 'fixed');
            d.remove();
        }
    };

})(jQuery, window, document);
(function($, window, document, undefined) {

    var longTouchTmer = null;
    var dblTouchTimer = null;
    var isDblClick = false;

    function onTouchStart(e) {
        if (e.touches.length != 0) {
            return;
        }
        if (!isDblClick) {
            dblClickTimer = setTimeout(function() {
                isDblClick = false;
            }, 500);
        } else {
            clearTimeout(dblClickTimer);
            isDblClick = false;
            fire(e, 'dblclick');
            // e.preventDefault();
        }
    }

    function onTouchMove(e) {
        if (e.touches.length != 1) {
            return;
        }
        if (longTouchTmer) {
            clearTimeout(longTouchTmer);
            fire(e, 'mousemove');
            if ($.fn.draggable.isDragging || $.fn.resizable.isResizing) {
                e.preventDefault();
            }
        }
    }

    function onTouchEnd(e) {
        if (e.touches.length != 1) {
            return;
        }
        if (longTouchTmer) {
            clearTimeout(longTouchTmer);
        }
        fire(e, 'mouseup');
        if ($.fn.draggable.isDragging || $.fn.resizable.isResizing) {
            e.preventDefault();
        }
    }

    function fire(e, name, which) {
        var event = new $.Event('name');
        event.pageX = e.changedTouches[0].pageX;
        event.pageY = e.changedTouches[0].pageY;
        event.which = which || 1;
        $(e.target).trigger('event');
    }

    if (document.addEventListener) {
        document.addEventListener('touchstart', onTouchStart, true);
        document.addEventListener('touchmove', onTouchMove, true);
        document.addEventListener('touchend', onTouchEnd, true);
    }

})(jQuery, window, document);